﻿using OpenCvSharp;

namespace OpenCvSharpStudy
{
    public class OpencvHelper
    {

        public static Mat LoadFile(string fileName)
        {
            Mat mat= Cv2.ImRead(fileName, ImreadModes.Color);
           
            return mat;
        }
        public static Mat ToGray(Mat mat)
        {
            Mat gray= new Mat();
            Cv2.CvtColor(mat, gray, ColorConversionCodes.BGR2GRAY);

            //Cv2.ImShow("mat", mat);
            //Cv2.ImShow("gray", gray);
            //Cv2.WaitKey();
            return gray;
            
        }
        public static Mat Resize(Mat mat)
        {
            
            Mat result = new Mat();
          //  Cv2.Resize(mat, result,  new OpenCvSharp.Size(300,300));
            Cv2.Resize(mat, result, new OpenCvSharp.Size(0, 0), 2,2);
            //Cv2.ImShow("mat", mat);
            //Cv2.ImShow("result", result);
            //Cv2.WaitKey();
            return result;
        }
        public static Mat ConvertToBlackBinary(Mat mat)
        {
           //Cv2.ImShow("mat", mat);
            Mat gray = new Mat();
            Cv2.CvtColor(mat, gray, ColorConversionCodes.BGR2GRAY);//转灰度图
            //Cv2.ImShow("gray", gray);
            Scalar scalar  = Cv2.Mean(gray);//计算灰度图平均值
            Cv2.Threshold(gray, gray, scalar.Val0, 255, ThresholdTypes.Binary);//二值化
            //Cv2.ImShow("mat", mat);
            //Cv2.ImShow("result", gray);
            Cv2.WaitKey();

            return gray;
            
        }
        public static Mat Add(Mat mat)
        {
            Cv2.ImShow("mat", mat);
            Mat gray = ConvertToBlackBinary(mat);
            Cv2.BitwiseNot(gray, gray);//颜色反转，所有点按照x=255-x运算
           // Cv2.ImShow("gray", gray);
          
            Mat mat1= new Mat();
            Cv2.CvtColor(gray, mat1, ColorConversionCodes.GRAY2BGR);//灰度图转彩色图，

          //  Cv2.BitwiseAnd(mat, mat1, mat1);
          //  Cv2.BitwiseOr(mat, mat1, mat1);
            Cv2.BitwiseXor(mat, mat1, mat1);
            Mat mat2 = mat.Clone();
            mat2.SetTo( new Scalar(2,2,2));    
            Cv2.Multiply(mat, mat2, mat2);
           
             Cv2.ImShow("mat2", mat2);
             Cv2.WaitKey();
            return mat1;
        }


        public static Mat GetColor(Mat mat)
        {
            //  HSV:H颜色范围
            // Orange  0 - 22
            //Yellow 22 - 38
            // Green 38 - 75
            // Blue 75 - 130
            // Violet 130 - 160
            // Red 160 - 179
            //Cv2.ImShow("mat", mat);
            Mat hsv = new Mat();//HSV格式对象
            Cv2.CvtColor(mat, hsv, ColorConversionCodes.BGR2HSV);//颜色转换
            var mask = hsv.InRange(new Scalar(0, 46, 46), new Scalar(25, 255, 255));//红色区域
            var mask2 = hsv.InRange(new Scalar(165, 46, 46), new Scalar(180, 255, 255));//红色区域
            Cv2.BitwiseOr(mask, mask2, mask);//范围大叠加
            Cv2.BitwiseNot(mask, mask);//取反
            //Cv2.ImShow("mask", mask);
            Mat mat1 = new Mat();
            Cv2.CvtColor(mask, mat1, ColorConversionCodes.GRAY2BGR);//灰度图转彩色图，
            Cv2.Add(mat, mat1, mat1);//与原图相加，获取红色的区域
            //Cv2.ImShow("mat1", mat1);
            Cv2.WaitKey();

            return mat1;
        }
        public static Mat GetHoughLinesP(Mat mat)
        {
            
            Mat gray = ToGray(mat);
            
            var kenal3 = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(1, 1));
            Cv2.Dilate(gray, gray, kenal3);//

          
            Cv2.Canny(gray, gray, 50, 150);
            
            var lines = Cv2.HoughLinesP(gray, 1, Math.PI / 180, 100, 250, 50);
            if (lines.Length > 0)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    Cv2.Line(mat, lines[i].P1, lines[i].P2, new Scalar(255,0,0),1, LineTypes.AntiAlias);
                }
            }
            
            return mat;
        }

        public static Mat Test(Mat mat)
        {
         
            return mat;
        }

        
    }
}
   